package com.freesky.biz.psbc.controller;

import com.alibaba.fastjson.JSONObject;
import com.freesky.biz.activity.service.ISmsCodeService;
import com.freesky.biz.psbc.domain.TPsbcCoupon;
import com.freesky.biz.psbc.service.IPsbcCouponService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 邮储发券列表
 */
@RestController
@RequestMapping("/psbc/coupon")
public class PsbcCouponController extends BaseController {

    @Autowired
    private IPsbcCouponService psbcCouponService;

    @Autowired
    private ISmsCodeService smsCodeService;

    @PreAuthorize("@ss.hasPermi('psbc:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPsbcCoupon psbcCoupon) {
        startPage();
        List<TPsbcCoupon> list = psbcCouponService.findList(psbcCoupon);
        return getDataTable(list);
    }

    @Log(title = "邮储发券", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('psbc:coupon:export')")
    @GetMapping("/export")
    public AjaxResult export(TPsbcCoupon psbcCoupon,
                             HttpServletResponse response) {
        return psbcCouponService.exportFile(psbcCoupon);
    }

    @PreAuthorize("@ss.hasPermi('psbc:coupon:edit')")
    @GetMapping("/query/{id}")
    public AjaxResult query(@PathVariable String id,
                             HttpServletResponse response) {
        String stockId = psbcCouponService.getPsbcCardByReceiveId(id);
//        if(StringUtils.isNotEmpty(stockId)){
//            stockId = stockId.replaceAll("(\\S{4})\\S{10}(\\S{4})", "$1****$2");
//        }
        return AjaxResult.success("成功",stockId);
    }

    //重新发放短信
    @PreAuthorize("@ss.hasPermi('psbc:coupon:edit')")
    @GetMapping("/sendCode/{id}")
    public AjaxResult sendCode(@PathVariable String id,
                            HttpServletResponse response) {
        TPsbcCoupon psbcCoupon = psbcCouponService.selectPsbcCouponById(id);
        if(psbcCoupon != null){
            String stockId = psbcCouponService.getPsbcCardByReceiveId(id);
            JSONObject jsonParams = new JSONObject();
            jsonParams.put("other_number2", stockId);
            smsCodeService.sendPsbcCardNo(psbcCoupon.getMobile(),"SMS_477310036",jsonParams.toJSONString(),"","邮储京东E卡");
            return AjaxResult.success("成功!");
        } else {
            return AjaxResult.error("操作记录不存在!");
        }
    }

}
